# Contributor: Piper McCorkle <piper@cloudflare.com>
# Maintainer: Piper McCorkle <piper@cloudflare.com>
pkgname=cloudflared
pkgver=2023.7.3
pkgrel=2
pkgdesc="Cloudflare Tunnel client"
url="https://github.com/cloudflare/cloudflared"
# TODO: Doesn't compile with Go 1.21.
# See: https://gitlab.alpinelinux.org/alpine/aports/-/issues/15252
#arch="aarch64 x86 x86_64"
arch=""
license="Apache-2.0"
makedepends="go gettext"
subpackages="$pkgname-doc $pkgname-openrc"
pkgusers="$pkgname"
pkggroups="$pkgname"
install="$pkgname.pre-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/cloudflare/cloudflared/archive/refs/tags/$pkgver.tar.gz
	goflags.patch
	config.yml
	$pkgname.initd
	$pkgname.confd
	"
options="!check" # require privileged icmp sockets

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

prepare() {
	default_prepare
	go mod vendor
}

build() {
	make cloudflared
	DATE="2023" VERSION="$pkgver" envsubst < cloudflared_man_template > cloudflared.1
}

check() {
	_goarch=$(go tool dist env | grep GOARCH | sed 's/^GOARCH="//; s/"$//')
	# Go race detector is only supported on amd64, ppc64le, and arm64
	if [ $_goarch = "amd64" ] || [ $_goarch = "ppc64le" ] || [ $_goarch = "arm64" ]; then
		_race=-race
	fi
	go test -mod=vendor -buildmode=default $_race ./...
}

package() {
	install -D -m755 ./cloudflared "$pkgdir"/usr/bin/cloudflared
	install -D -m644 ./cloudflared.1 "$pkgdir"/usr/share/man/man1/cloudflared.1

	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -D -m755 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -D -m644 "$srcdir"/config.yml "$pkgdir"/etc/$pkgname/config.yml
}

sha512sums="
45833cfd35ad8458882008dc73878948955584a1ce26ff63126b7045d5807d67d275c56ac5d39695f42839daf948a78c894c28f692183e92674d43d9254d7534  cloudflared-2023.7.3.tar.gz
deb54b26ac89da74f6d1016deebdd2a15e302d49c1c2b83787b905e70b00250ddf9568d078c4d1d7b53bba9ab1aa182fbcadca2376a7008f07ce4502a4c6f91c  goflags.patch
01f43af92c2c804f6b31189b78f81a76f078179065bebf12118a8bb3ba22e1c8042ef760f24acdf1356bbe5235df7d2a00d55a158e5fec5063c5d2d6abcd7dee  config.yml
76b684ef4d0267e8546a21cc60239ef94cbb5fd6b3c0f4fbf4e843eb6e8b56943d3200a160e1749b228306e5649e6a1fa90a58f14fb68c5794f2c760a6c14506  cloudflared.initd
47c71a328d49f3d35dd6e0268e1d734cd21ecc84b24459f77a46df7632f9a8fdb7817cf49187f965c13c94017e4454897caa99ca12b41037dfe0455bb156ef09  cloudflared.confd
"
